package com.woniuxy.lab.woniu_lab.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.lab.woniu_lab.commons.Result;
import com.woniuxy.lab.woniu_lab.controller.form.AddCommentForm;
import com.woniuxy.lab.woniu_lab.service.CommentService;
import com.woniuxy.lab.woniu_lab.service.dto.CommentDto;
import com.woniuxy.lab.woniu_lab.service.dto.ParentCommentDto;
import com.woniuxy.lab.woniu_lab.util.JwtUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/comment")
@Slf4j
@Api(tags = "[C]评论与回复相关接口")
@Validated
public class CommentController {
    @Resource
    private CommentService commentService;

    /**
     * 查看我的评论的未读回复
     * @param
     * @return
     */
    @ApiOperation("02 查询对我评论的回复")
    @GetMapping("/findComment")
    public Result<IPage<CommentDto>> findComment(@ApiParam(value = "第几页") @NotNull(message = "当前页不能为空")
                                                     @Min(value = 1,message = "当前页最小为1") Integer page,
                                                 @ApiParam(value = "每页条数") @NotNull(message = "展示数量不能为空")
                                                 @Min(value = 1,message ="展示数量最小为1") @Max(value = 50,message = "展示数量最大为50") Integer size) {
        Integer userId = JwtUtil.getUserIdFromReq();
        log.info("根用户ID:{}查询所有未读评论", userId);
        IPage<CommentDto> comment = commentService.findComment(userId, page, size);
        return Result.success(200, "查询评论成功", comment);
    }

    @ApiOperation("03 消息置为已读")
    @PutMapping("/readComment")
    public Result<Object> readComment(@ApiParam(value = "评论id") @RequestParam("commentId")
                                          @NotNull(message = "评论id不能为空") @Min(value = 1,message = "评论id最小为1") Integer commentId) {
        log.info("将评论{}改为已读状态", commentId);
        commentService.read(commentId);
        return Result.success(200, "已读成功", null);
    }

    /**
     * 查询评论
     * @param rangeId 靶场ID
     * @return ParentCommentDto 实验详情
     */
    @ApiOperation("52 获取该实验的评论")
    @GetMapping("/queryComment")
    public Result<List<ParentCommentDto>> queryComment(@ApiParam(value = "靶场ID") @NotNull(message = "靶场ID不能为空")
                                                           @Min(value = 1,message = "靶场ID最小为1") Integer rangeId){
        log.info("获取该实验的评论：{}",rangeId);
        List<ParentCommentDto> parentCommentDtos = commentService.queryComment(rangeId);
        return Result.success(200,"获取该实验的评论成功",parentCommentDtos);

    }

    /**
     * 评论实验/回复评论
     * @param addComment 评论/回复 参数
     */
    @ApiOperation("53 评论实验/回复评论")
    @PostMapping("/addComment")
    public Result<Object> addComment(@Valid @RequestBody AddCommentForm addComment){
        log.info("开始评论该靶场/回复该靶场的评论：{}",addComment);
        commentService.addComment(addComment);
        return Result.success(200,"开始评论该靶场/回复该靶场的评论成功",null);
    }
}

